package com.atguigu.blog.service.impl;

import com.atguigu.blog.mapper.CategoryMapper;
import com.atguigu.blog.pojo.Category;
import com.atguigu.blog.service.CategoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author MikeCoder
 * @create 2023-11-2419:27
 * @description:
 * @verson:
 */
@Service
@Transactional
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    @Transactional(readOnly = true)
    public List<Category> showAll() {
        return categoryMapper.selectAll();
    }

    @Override
    @Transactional(readOnly = true)
    public Map<String,Object> showByCname(Integer limit,Integer page,Category category) {
        PageHelper.startPage(page,limit);
        List<Category> categoryList = categoryMapper.selectCategoryByCname(category);
        PageInfo<Category> info = new PageInfo<>(categoryList);

        Map<String,Object> map = new HashMap<>();
        map.put("list",categoryList);
        map.put("total",info.getTotal());
        return map;
    }

    @Override
    public Integer add(Category category) {
        return categoryMapper.insert(category);
    }

    @Override
    public Integer change(Category category) {
        return categoryMapper.update(category);
    }

    @Override
    @Transactional(readOnly = true)
    public Category getCategoryByCid(Long cid) {
        return categoryMapper.selectCategoryByCid(cid);
    }

    @Override
    public Integer remove(Long cid) {
        return categoryMapper.delete(cid);
    }
}
